/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifndef PL_KERNEL_VECTOR_H
#define PL_KERNEL_VECTOR_H

#include <stddef.h>
using namespace std;

/**
 * A simple vector implementation for the kernel.
 */
template< typename T >
class PLKVector {
public:
	/**
	 * Constructor.
	 *
	 * @param size The start size of the vector.
	 * @param expand The value to use for expanding the vector.
	 */
	PLKVector(uint32_t size = 10, uint32_t expand = 10);
	/**
	 * Append data to the vector.
	 *
	 * @param data The data to add.
	 */
	inline void append(T data);
	/**
	 * Get the object at a position.
	 *
	 * @param pos The position to get.
	 * @return The object.
	 */
	inline T at(uint32_t pos);
	/**
	 * Get a element from the vector and remove it from the vector.
	 *
	 * @param pos The element to get.
	 * @return The element.
	 */
	inline T take(uint32_t pos);
	/**
	 * Remove a element from the vector.
	 *
	 * @param pos The position of the element to remove.
	 */
	inline void remove(uint32_t pos);
	/**
	 * Delete a object from the vector.
	 *
	 * @param obj The object to delete.
	 */
	inline void remove(T obj);
	/**
	 * The count of elements in the vector.
	 *
	 * @return The count.
	 */
	inline uint32_t count();
	/**
	 * The size of the memory that was allocated.
	 *
	 * @return The size.
	 */
	inline uint32_t size();
private:
	inline void expand(uint32_t resizeTo);
	uint32_t m_size;
	uint32_t m_count;
	uint32_t m_expand;
	T *m_data;
};

template < typename T >
PLKVector<T>::PLKVector(uint32_t _size, uint32_t _expand) : m_size(_size), m_count(0), m_expand(_expand), m_data(new T[_size]) {
}

template < typename T >
void PLKVector<T>::append(T data) {
	expand(1);
	m_data[m_count] = data;
	m_count++;
}

template < typename T >
T PLKVector<T>::at(uint32_t pos) {
	if(pos >= m_count) {
		return T();
	}
	return m_data[pos];
}

template < typename T >
T PLKVector<T>::take(uint32_t pos) {
	if(pos >= m_count) {
		return T();
	}
	T ret = at(pos);
	remove(pos);
	return ret;
}

template < typename T >
void PLKVector<T>::remove(uint32_t pos) {
	for(uint32_t i = pos ; i < m_count - 1 ; i++) {
		m_data[i] = m_data[i + 1];
	}
	m_count--;
}

template < typename T >
void PLKVector<T>::remove(T obj) {
	for(uint32_t i = 0 ; i < m_count ; i++) {
		if(m_data[i] == obj) {
			remove(i);
			return;
		}
	}
}

template < typename T >
uint32_t PLKVector<T>::count() {
	return m_count;
}

template < typename T >
uint32_t PLKVector<T>::size() {
	return m_size;
}

template < typename T >
void PLKVector<T>::expand(uint32_t resizeTo) {
	if(m_count + resizeTo > m_size) {
		T *tmp = m_data;
		m_data = new T[m_size + m_expand];
        m_size += m_expand;
		for(uint32_t i = 0 ; i < m_count ; i++) {
			m_data[i] = tmp[i];
		}
		expand(resizeTo);
        delete[] tmp;
	}
}

#endif
